/**
 * 组件展示公共js
 */

import TitleCommon from "@/views/modules/preview/components/common/title-common.vue";

const ComponentDef = {
	props:["config","screenConfig","drillDownValue","loadView"],
	components:{
        TitleCommon
	},
	provide(){
		return{
			chartConfig:this.config
		}
	},
	data() {
		return {}
	},
	mounted() {
		//获取数据
		if(this.getData){
			this.getData();
		}
		//实时刷新配置
		let refreshConfig=this.config.advancedConf?this.config.advancedConf.refreshConfig:"";
		if(refreshConfig && refreshConfig.isRefresh && refreshConfig.interval){
			let self=this;
			let ms=0;
			//分钟
			if(refreshConfig.unit=="minute"){
				ms=60000*refreshConfig.interval;
			}else {
				ms=1000*refreshConfig.interval;
			}
			setInterval(function () {
				//获取数据
				if(self.getData){
					self.getData();
				}else if(self.$refs[self.chartComponent] && self.$refs[self.chartComponent].initChart){
					self.$refs[self.chartComponent].initChart();
				}
			},ms);
		}
	},
	methods: {
		tableI18n(name,text,key){
			return this.$ti18(name,text,"tableLang",key);
		},
		formatTemplateField(value,data){
			// 分别将json数据 data 中的键名和键值取出存储
			const keys = Object.keys(data);
			const dataList = keys.map(function(key) {
				return data[key];
			});
			// 遍历键名：判断字符串value中是否包含对应键名。若有，则替换为相应的键值。
			for (let i = 0; i < keys.length; i++) {
				value = value.replace(
					new RegExp("\\$\\{" + keys[i] + "\\}", "gm"),
					dataList[i]
				);
			}
			return value;
		},
		gestalt(list){
			this.config.dataSources.measures.filter(res=>{
				if(res.gestalt){
					let fieldName = res.tableName + '_' + res.fieldName;
					let value = list[fieldName];
					this.data[0][fieldName] = this.formattedData(value,res.gestalt)
				}
			})
		},
		//格式化数据
		formattedData(value,configuration) {
			if (configuration.round) {
				value = this.roundFixed(value,configuration.decimals)
			}else {
				//先处理小数位
				value = this.toFixed(value, configuration.decimals);
			}
			if (configuration.dataFormat && configuration.dataFormat == 'thousands') {
				//千分位
				value = (value + '').replace(/(\d{1,3})(?=(\d{3})+(?:$|\.))/g, '$1,');

			} else if (configuration.dataFormat && configuration.dataFormat == 'percentage') {
				//百分比
				value = (value * 100) + '%';
			} else if (configuration.dataFormat && configuration.dataFormat == 'trillion') {
				//千分位
				if (configuration.decimals && configuration.decimals == 'none') {
					value = this.thousands(value);
				} else {
					value = this.thousands(value, configuration);
				}
			}
			return value;
		},
		//转千万亿
		thousands(value,decimals){
			let newValue=0;
			if(!decimals){
				if (value >= 100000000) {
					newValue=value / 100000000 + "亿";
				} else if (value >= 10000000) {
					newValue=value / 10000000 + "千万";
				} else if (value >= 1000000) {
					newValue=value / 1000000 + "百万";
				}else if (value >= 10000) {
					newValue=value / 10000 + "万";
				} else if (value >= 1000) {
					newValue=value / 1000 + "千";
				} else {
					newValue=value;
				}
			}else if(decimals.round){
				if (value >= 100000000) {
					newValue=this.roundFixed(value / 100000000,decimals.decimals) + "亿";
				} else if (value >= 10000000) {
					newValue=this.roundFixed(value / 10000000,decimals.decimals) + "千万";
				} else if (value >= 1000000) {
					newValue=this.roundFixed(value / 1000000,decimals.decimals) + "百万";
				}else if (value >= 10000) {
					newValue=this.roundFixed(value / 10000,decimals.decimals) + "万";
				} else if (value >= 1000) {
					newValue=this.roundFixed(value / 1000,decimals.decimals) + "千";
				} else {
					newValue=this.roundFixed(value,decimals.decimal);
				}
			}else {
				if (value >= 100000000) {
					newValue=this.toFixed(value / 100000000,decimals.decimals) + "亿";
				} else if (value >= 10000000) {
					newValue=this.toFixed(value / 10000000,decimals.decimals) + "千万";
				} else if (value >= 1000000) {
					newValue=this.toFixed(value / 1000000,decimals.decimals) + "百万";
				}else if (value >= 10000) {
					newValue=this.toFixed(value / 10000,decimals.decimals) + "万";
				} else if (value >= 1000) {
					newValue=this.toFixed(value / 1000,decimals.decimals) + "千";
				} else {
					newValue=this.toFixed(value,decimals.decimal);
				}
			}
			return newValue;
		},
		//保留小数
		toFixed(value, type) {
			switch (type) {
				case 'roundNumbers':
					value = Math.floor(value * 1) / 1;
					break;
				case 'oneDecimals':
					value = Math.floor(value * 10) / 10;
					break;
				case 'twoDecimals':
					value = Math.floor(value * 100) / 100;
					break;
				case 'threeDecimals':
					value = Math.floor(value * 1000) / 1000;
					break;
			}
			return value
		},
		roundFixed(num, type) {//修改js四舍五入
			let fixed = 0;
			switch (type) {
				case 'roundNumbers':
					fixed = 0;
					break;
				case 'oneDecimals':
					fixed = 1;
					break;
				case 'twoDecimals':
					fixed = 2;
					break;
				case 'threeDecimals':
					fixed = 3;
					break;
			}
			return parseFloat(num).toFixed(fixed);
		},
        getImage(url) {
            return import.meta.env.JPAAS_BASE_URL + url;
        },
	},
	watch:{
		config:{
			handler(){
				if(this.getData){
					this.getData();
				}
			},
			deep:true
		}
	}

}

export default ComponentDef;
